package com.by.wechatsend;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *  @Description 获取句子
 * @author baiyu
 * @date 2022/5/8 - 12:00
 */

public class getSencents {
    /**
     * @Description 随机一句话
     * @author baiyu
     * @date 2022/5/8 - 12:00
     */
    public static String getSencents() {
        String str = read.getRead();

        //根据句号和数字分割字符串
        String[] split = str.split("[.\\d]");
        //去掉字符串为空的数组
        String[] newSplit = removeNullStringArray(split);

        Random random = new Random();
        //随机获取split数组的下标
        int i = random.nextInt(newSplit.length);
        //根据下标来反馈对应的字符串元素
        return newSplit[i];
    }

    /**
     * @Description 数组去空
     *@author baiyu
     * @date 2022/5/8 - 12:00
     */
    public static String[] removeNullStringArray(String[] arrayString) {
        List<String> list1 = new ArrayList<String>();
        for (int i = 0; i < arrayString.length; i++) {
            //过滤掉数组arrayString里面的空字符串
            if (arrayString[i] != null && arrayString[i].length() != 0) {
                list1.add(arrayString[i]);
            }
        }
        //集合转换为String数组
        return list1.toArray(new String[list1.size()]);
    }
}

